import MagicString from 'magic-string';

const removeHash = {
  'defineProperty': [/^\s*Object\.defineProperty\(\s*exports,\s*'__esModule'.*\n$/mg, ''],
  'freeze': [/Object.freeze\s*\(\s*([^)]*)\)/g, '$1']
}
const remove = Object.keys(removeHash)

export default (code) => {
  console.log(typeof(code))
  const magicString = new MagicString(code);
  for (let k in removeHash) {
    if (remove.indexOf(k) > -1) {
      const [pattern, value = ''] = removeHash[k];
      let match, start, end, replacement;

      while ((match = pattern.exec(code))) {
        start = match.index;
        end = start + match[0].length;
        replacement = value.toString();
        for (let i = 1; i < match.length; i++) {
          replacement = replacement.replace(`$${i}`, match[i]);
        }
        magicString.overwrite(start, end, replacement);
      }
    }
  }
  return code //magicString.toString();
}
